package com.appcenter.application.service;

import com.appcenter.application.expand.ObjectFinal;
import com.appcenter.application.input.assortcode.AddAssortCodeInput;
import com.appcenter.application.input.assortcode.GetAssortCodeInput;
import com.appcenter.application.mapper.AssortCodesMapper;
import com.appcenter.application.model.AssortCodes;
import com.appcenter.application.output.assortcode.AddAssortCodeOutput;
import com.appcenter.application.output.assortcode.GetAssortCodeOutput;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.unit.ObjectUnit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AssortCodeService {
    @Autowired
    AssortCodesMapper assortCodesMapper;

    //分配验证码
    public AddAssortCodeOutput AddAssortCode(AddAssortCodeInput input) throws Exception {
        AddAssortCodeOutput output = new AddAssortCodeOutput();
        //查询是否已存在生效的验证码
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, ObjectFinal.CODE_EXPIRED);
        Example example = new Example(AssortCodes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userCode", input.getUserCode());
        criteria.andEqualTo("genre", input.getGenre());
        //criteria.andLessThan("createTime", nowTime.getTime());
        List<AssortCodes> list = assortCodesMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            for (int i = 0; i <list.size() ; i++) {
                assortCodesMapper.deleteByPrimaryKey(list.get(i));
            }
        }
        AssortCodes assortCodes = new AssortCodes();
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        assortCodes.setId(uuid);
        assortCodes.setGenre(input.getGenre());
        assortCodes.setUserCode(input.getUserCode());
        assortCodes.setCreateTime(new Date());
        int a=(int)((Math.random()*9+1)*100000);
        assortCodes.setVerificationCode(Integer.toString(a));
        assortCodesMapper.insertSelective(assortCodes);
        BusinessLogger.Info(LoggerGenre.AssortCode,input.getGenre()+"-分配验证码",input);
        output.setResult(Integer.toString(a));
        return output;
    }

    //查询验证码信息
    public GetAssortCodeOutput GetAssortCode(GetAssortCodeInput input) throws Exception {
        GetAssortCodeOutput output = new GetAssortCodeOutput();

        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, ObjectFinal.CODE_EXPIRED);
        Example example = new Example(AssortCodes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userCode", input.getUserCode());
        criteria.andEqualTo("genre", input.getGenre());
        criteria.andGreaterThan("createTime", nowTime.getTime());
        List<AssortCodes> list = assortCodesMapper.selectByExample(example);
        if(list==null || list.size()==0){
            throw  new ErrorJsonResult("10000031","未找到验证码信息,请重新发送");
        }
        if(list.size()>1){
            throw  new ErrorJsonResult("10000031","发现多个符合条件的验证码,请联系管理员");
        }
        BusinessLogger.Info(LoggerGenre.View,"获取验证码",input);
        BeanUtils.copyProperties(list.get(0),output);
        return output;
    }




}
